package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.OtherSafeguardService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.OtherSafeguardDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing OtherSafeguard.
 */
@RestController
@RequestMapping("/api")
public class OtherSafeguardResource {

    private final Logger log = LoggerFactory.getLogger(OtherSafeguardResource.class);

    private static final String ENTITY_NAME = "otherSafeguard";

    private final OtherSafeguardService otherSafeguardService;

    public OtherSafeguardResource(OtherSafeguardService otherSafeguardService) {
        this.otherSafeguardService = otherSafeguardService;
    }

    /**
     * POST  /other-safeguards : Create a new otherSafeguard.
     *
     * @param otherSafeguardDTO the otherSafeguardDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new otherSafeguardDTO, or with status 400 (Bad Request) if the otherSafeguard has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/other-safeguards")
    @Timed
    public ResponseEntity<OtherSafeguardDTO> createOtherSafeguard(@RequestBody OtherSafeguardDTO otherSafeguardDTO) throws URISyntaxException {
        log.debug("REST request to save OtherSafeguard : {}", otherSafeguardDTO);
        if (otherSafeguardDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new otherSafeguard cannot already have an ID")).body(null);
        }
        OtherSafeguardDTO result = otherSafeguardService.save(otherSafeguardDTO);
        return ResponseEntity.created(new URI("/api/other-safeguards/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /other-safeguards : Updates an existing otherSafeguard.
     *
     * @param otherSafeguardDTO the otherSafeguardDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated otherSafeguardDTO,
     * or with status 400 (Bad Request) if the otherSafeguardDTO is not valid,
     * or with status 500 (Internal Server Error) if the otherSafeguardDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/other-safeguards")
    @Timed
    public ResponseEntity<OtherSafeguardDTO> updateOtherSafeguard(@RequestBody OtherSafeguardDTO otherSafeguardDTO) throws URISyntaxException {
        log.debug("REST request to update OtherSafeguard : {}", otherSafeguardDTO);
        if (otherSafeguardDTO.getId() == null) {
            return createOtherSafeguard(otherSafeguardDTO);
        }
        OtherSafeguardDTO result = otherSafeguardService.save(otherSafeguardDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, otherSafeguardDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /other-safeguards : get all the otherSafeguards.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of otherSafeguards in body
     */
    @GetMapping("/other-safeguards")
    @Timed
    public ResponseEntity<List<OtherSafeguardDTO>> getAllOtherSafeguards(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of OtherSafeguards");
        Page<OtherSafeguardDTO> page = otherSafeguardService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/other-safeguards");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /other-safeguards/:id : get the "id" otherSafeguard.
     *
     * @param id the id of the otherSafeguardDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the otherSafeguardDTO, or with status 404 (Not Found)
     */
    @GetMapping("/other-safeguards/{id}")
    @Timed
    public ResponseEntity<OtherSafeguardDTO> getOtherSafeguard(@PathVariable Long id) {
        log.debug("REST request to get OtherSafeguard : {}", id);
        OtherSafeguardDTO otherSafeguardDTO = otherSafeguardService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(otherSafeguardDTO));
    }

    /**
     * DELETE  /other-safeguards/:id : delete the "id" otherSafeguard.
     *
     * @param id the id of the otherSafeguardDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/other-safeguards/{id}")
    @Timed
    public ResponseEntity<Void> deleteOtherSafeguard(@PathVariable Long id) {
        log.debug("REST request to delete OtherSafeguard : {}", id);
        otherSafeguardService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/other-safeguards?query=:query : search for the otherSafeguard corresponding
     * to the query.
     *
     * @param query the query of the otherSafeguard search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/other-safeguards")
    @Timed
    public ResponseEntity<List<OtherSafeguardDTO>> searchOtherSafeguards(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of OtherSafeguards for query {}", query);
        Page<OtherSafeguardDTO> page = otherSafeguardService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/other-safeguards");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
